/**
 * @file main.h
 * @brief Main header file for H743-V2 Flight Control System
 * @author Flight Control Team
 * @date 2025-10-31
 */

#ifndef __MAIN_H
#define __MAIN_H

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>

/* STM32 HAL includes */
#include "stm32h7xx_hal.h"

/* Hardware configuration */
#define SYSTEM_CLOCK_HZ                 480000000U  // 480MHz
#define APB1_CLOCK_HZ                   120000000U  // 120MHz
#define APB2_CLOCK_HZ                   120000000U  // 120MHz
#define APB3_CLOCK_HZ                   120000000U  // 120MHz
#define APB4_CLOCK_HZ                   120000000U  // 120MHz

/* LED GPIO pins */
#define LED_GREEN_PORT                  GPIOE
#define LED_GREEN_PIN                   2           // PE2
#define LED_RED_PORT                    GPIOE
#define LED_RED_PIN                     3           // PE3
#define LED_BLUE_PORT                   GPIOE
#define LED_BLUE_PIN                    4           // PE4

/* Buzzer GPIO pin */
#define BUZZER_PORT                     GPIOD
#define BUZZER_PIN                      15          // PD15

/* SPI2 - BMI088 IMU */
#define SPI2_SCK_PORT                   GPIOD
#define SPI2_SCK_PIN                    3           // PD3
#define SPI2_MISO_PORT                  GPIOC
#define SPI2_MISO_PIN                   2           // PC2
#define SPI2_MOSI_PORT                  GPIOC
#define SPI2_MOSI_PIN                   3           // PC3
#define BMI088_ACCEL_CS_PORT            GPIOD
#define BMI088_ACCEL_CS_PIN             4           // PD4
#define BMI088_GYRO_CS_PORT             GPIOD
#define BMI088_GYRO_CS_PIN              5           // PD5
#define BMI088_ACCEL_INT_PORT           GPIOC
#define BMI088_ACCEL_INT_PIN            14          // PC14
#define BMI088_GYRO_INT_PORT            GPIOC
#define BMI088_GYRO_INT_PIN             15          // PC15

/* SPI3 - BMI270 IMU */
#define SPI3_SCK_PORT                   GPIOB
#define SPI3_SCK_PIN                    3           // PB3
#define SPI3_MISO_PORT                  GPIOB
#define SPI3_MISO_PIN                   4           // PB4
#define SPI3_MOSI_PORT                  GPIOD
#define SPI3_MOSI_PIN                   6           // PD6
#define BMI270_CS_PORT                  GPIOA
#define BMI270_CS_PIN                   15          // PA15
#define BMI270_INT_PORT                 GPIOB
#define BMI270_INT_PIN                  7           // PB7

/* I2C1 - External (Magnetometer) */
#define I2C1_SCL_PORT                   GPIOB
#define I2C1_SCL_PIN                    8           // PB8
#define I2C1_SDA_PORT                   GPIOB
#define I2C1_SDA_PIN                    9           // PB9

/* I2C2 - Internal (Barometer, Power Monitor) */
#define I2C2_SCL_PORT                   GPIOB
#define I2C2_SCL_PIN                    10          // PB10
#define I2C2_SDA_PORT                   GPIOB
#define I2C2_SDA_PIN                    11          // PB11
#define SPL06_INT_PORT                  GPIOD
#define SPL06_INT_PIN                   0           // PD0

/* SDMMC1 - SD Card */
#define SDMMC1_CK_PORT                  GPIOC
#define SDMMC1_CK_PIN                   12          // PC12
#define SDMMC1_CMD_PORT                 GPIOD
#define SDMMC1_CMD_PIN                  2           // PD2
#define SDMMC1_D0_PORT                  GPIOC
#define SDMMC1_D0_PIN                   8           // PC8
#define SDMMC1_D1_PORT                  GPIOC
#define SDMMC1_D1_PIN                   9           // PC9
#define SDMMC1_D2_PORT                  GPIOC
#define SDMMC1_D2_PIN                   10          // PC10
#define SDMMC1_D3_PORT                  GPIOC
#define SDMMC1_D3_PIN                   11          // PC11

/* USB OTG FS */
#define USB_DM_PORT                     GPIOA
#define USB_DM_PIN                      11          // PA11
#define USB_DP_PORT                     GPIOA
#define USB_DP_PIN                      12          // PA12
#define USB_VBUS_PORT                   GPIOA
#define USB_VBUS_PIN                    8           // PA8

/* UART definitions */
#define UART_DEBUG                      USART6      // PC6/PC7
#define UART_GPS1                       USART2      // PA2/PA3
#define UART_GPS2                       USART3      // PD8/PD9
#define UART_TELEM1                     UART4       // PA0/PA1
#define UART_TELEM2                     UART5       // PB6/PB5
#define UART_TELEM3                     UART7       // PE8/PE7
#define UART_TELEM4                     UART8       // PE1/PE0
#define UART_RC                         USART1      // PA9/PA10

/* ADC channels */
#define ADC_VBAT_CHANNEL                10          // PC0 - ADC1_INP10
#define ADC_IBAT_CHANNEL                11          // PC1 - ADC1_INP11

/* System status flags */
#define STATUS_FLAG_SYSTEM_READY        (1 << 0)
#define STATUS_FLAG_SENSORS_OK          (1 << 1)
#define STATUS_FLAG_SD_CARD_OK          (1 << 2)
#define STATUS_FLAG_USB_CONNECTED       (1 << 3)
#define STATUS_FLAG_GPS_FIX             (1 << 4)
#define STATUS_FLAG_ARMED               (1 << 5)
#define STATUS_FLAG_ERROR               (1 << 6)

/* Error codes */
typedef enum {
    ERROR_NONE = 0,
    ERROR_SENSOR_INIT,
    ERROR_SD_CARD_INIT,
    ERROR_USB_INIT,
    ERROR_MEMORY_ALLOC,
    ERROR_TASK_CREATE,
    ERROR_SENSOR_TIMEOUT,
    ERROR_SENSOR_DATA_INVALID,
    ERROR_CONTROL_OVERFLOW,
    ERROR_UNKNOWN
} ErrorCode_t;

/* System state */
typedef enum {
    SYSTEM_STATE_INIT = 0,
    SYSTEM_STATE_IDLE,
    SYSTEM_STATE_READY,
    SYSTEM_STATE_ARMED,
    SYSTEM_STATE_FLYING,
    SYSTEM_STATE_ERROR,
    SYSTEM_STATE_SHUTDOWN
} SystemState_t;

/* Flight mode */
typedef enum {
    FLIGHT_MODE_MANUAL = 0,
    FLIGHT_MODE_STABILIZE,
    FLIGHT_MODE_ALTITUDE_HOLD,
    FLIGHT_MODE_POSITION_HOLD,
    FLIGHT_MODE_AUTO,
    FLIGHT_MODE_RTL,
    FLIGHT_MODE_LAND
} FlightMode_t;

/* Vehicle type */
typedef enum {
    VEHICLE_TYPE_QUADCOPTER = 0,
    VEHICLE_TYPE_HEXACOPTER,
    VEHICLE_TYPE_OCTOCOPTER,
    VEHICLE_TYPE_FIXED_WING,
    VEHICLE_TYPE_VTOL
} VehicleType_t;

/* System information structure */
typedef struct {
    SystemState_t state;
    FlightMode_t flight_mode;
    VehicleType_t vehicle_type;
    uint32_t status_flags;
    ErrorCode_t last_error;
    uint32_t uptime_ms;
    uint32_t loop_counter;
    float cpu_usage;
} SystemInfo_t;

/* Function prototypes */
void Error_Handler(void);

/* Utility macros */
#define UNUSED(x) ((void)(x))
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define CLAMP(x, min, max) (MIN(MAX((x), (min)), (max)))
#define DEG_TO_RAD(deg) ((deg) * 0.017453292519943295f)
#define RAD_TO_DEG(rad) ((rad) * 57.29577951308232f)

/* Timing macros */
#define MS_TO_TICKS(ms) ((ms) / portTICK_PERIOD_MS)
#define TICKS_TO_MS(ticks) ((ticks) * portTICK_PERIOD_MS)

/* Debug macros */
#ifdef DEBUG
#define DEBUG_PRINT(fmt, ...) printf("[DEBUG] " fmt "\r\n", ##__VA_ARGS__)
#define DEBUG_ASSERT(expr) if (!(expr)) { Error_Handler(); }
#else
#define DEBUG_PRINT(fmt, ...)
#define DEBUG_ASSERT(expr)
#endif

#ifdef __cplusplus
}
#endif

#endif /* __MAIN_H */

